package com.leyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.dto.PageDTO;
import com.leyou.entity.Brand;
import com.leyou.entity.CategoryAndBrand;
import com.leyou.item.dto.BrandDTO;
import com.leyou.mapper.BrandMapper;
import com.leyou.service.BrandService;
import com.leyou.service.CategoryAndBandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {

    @Autowired
    private CategoryAndBandService categoryAndBandService;
    @Override
    public List<BrandDTO> getBrandsByBrandIds(List<Long> brandIds) {

        //根据品牌id的集合查询品牌的集合
        List<Brand> brands = baseMapper.selectBatchIds(brandIds);
        //将品牌entity的集合转化为branddto集合
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brands);
        return brandDTOS;
    }

    @Override
    public List<BrandDTO> getBrandsByCategoryId(Long cid) {

        List<Long> category_id = categoryAndBandService
                .query()
                .eq(null != cid, "category_id", cid)
                .list()
                .stream()
                .map(categoryAndBrand -> categoryAndBrand.getBrandId())
                .collect(Collectors.toList());

        List<Brand> brands = baseMapper.selectBatchIds(category_id);

        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brands);


        return brandDTOS;
    }

    @Override
    public PageDTO<BrandDTO> getBrandPage(String key, String currentPage, String pageSize) {
        PageDTO<BrandDTO> brandDTOPageDTO = new PageDTO<>();

        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(null!=key, "name", key);
        Page<Brand> page = page(new Page<Brand>(Long.valueOf(currentPage), Long.valueOf(pageSize)), queryWrapper);
        brandDTOPageDTO.setItems(BrandDTO.convertEntityList(page.getRecords()));
        brandDTOPageDTO.setTotal(page.getTotal());
        brandDTOPageDTO.setTotalPage(page.getPages());
        return brandDTOPageDTO;
    }

    @Override
    public BrandDTO getBrandByBrandId(Long brandId) {
        Brand brand = baseMapper.selectById(brandId);
        BrandDTO brandDTO = new BrandDTO(brand);
        return brandDTO;
    }

    @Override
    public void addBrand(Brand brand) {
        boolean save = save(brand);
        if (save) {
            //保存成功，主键回显
            Long brandId = brand.getId();
            List<Long> categoryIds = brand.getCategoryIds();
            for (Long categoryId : categoryIds) {
                CategoryAndBrand categoryAndBrand = CategoryAndBrand.of(categoryId, brandId);
                categoryAndBandService.save(categoryAndBrand);
            }
        }

    }

    @Override
    public void updateBrand(Brand brand) {
        saveOrUpdate(brand);
        List<Long> categoryIds = brand.getCategoryIds();
        Long brandId = brand.getId();
        //先删除第三张表含有brandId的字段
        categoryAndBandService.remove(new QueryWrapper<CategoryAndBrand>().eq("brand_id", brandId));

        //再添加
        List<CategoryAndBrand> categoryAndBrands = categoryIds.stream()
                .map(categoryId -> CategoryAndBrand.of(categoryId, brandId))
                .collect(Collectors.toList());
        categoryAndBandService.saveBatch(categoryAndBrands);
    }

}
